﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using IniTechCaash.Domain.Entities;

namespace IniTechCaash.WebApplication.Models
{
    #region Create

    public class FinanceAccountCreateViewModel : AuditableEntityViewModel
    {
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }
        public override string Descriptive
        {
            get { return "ACT"; }
        }
        [Display(Name = "Enabled")]
        public bool IsEnabled { get; set; }
        [DataType(DataType.Text)]
        [Required]
        [StringLength(256)]
        public string Name { get; set; }
        public string Number { get; set; }
        public AccountType Type { get; set; }

        public Account GetAccount(string userName)
        {
            DateTime timeStamp = DateTime.Now;
            return new Account
            {
                Balance = 0.00m,
                CreatedBy = userName,
                CreatedDate = timeStamp,
                Description = this.Description,
                Id = this.Id,
                IsEnabled = this.IsEnabled,
                LastModifiedBy = userName,
                LastModifiedDate = timeStamp,
                Name = this.Name,
                Number = this.Number,
                Type = this.Type,
            };
        }
    }

    #endregion

    #region Details

    public class FinanceAccountDetailsViewModel : AuditableEntityViewModel
    {
        public FinanceAccountDetailsViewModel(Account account)
        {
            ActivityLogs = new List<ActivityLog>();
            Balance = account.Balance;
            CreatedBy = account.CreatedBy;
            CreatedDate = account.CreatedDate;
            Description = account.Description;
            Id = account.Id;
            IsEnabled = account.IsEnabled;
            Name = account.Name;
            Number = account.Number;
            LastModifiedBy = account.LastModifiedBy;
            LastModifiedDate = account.LastModifiedDate;
            Lines = new List<JournalEntryLineDetailsViewModel>(account.JournalEntryLines.Count);
            Type = account.Type;

            foreach (var line in account.JournalEntryLines)
                Lines.Add(new JournalEntryLineDetailsViewModel()
                {
                    Amount = line.Amount,
                    Date = line.Date,
                    Description = line.Description,
                    JournalEntry = new JournalEntryDetailsViewModel
                    {
                        DatePosted = line.JournalEntry.DatePosted,
                        Id = line.JournalEntry.Id,
                        IsPosted = line.JournalEntry.IsPosted,
                    },
                    Side = line.Side,
                });
                
        }

        public List<ActivityLog> ActivityLogs { get; set; }

        [DisplayFormat(DataFormatString = "{0:#,###0.00}")]
        public decimal Balance { get; set; }
        public string Description { get; set; }
        public override string Descriptive
        {
            get { return "ACT"; }
        }
        public string CompositeId
        {
            get
            {
                return String.Join("-", Descriptive, Number) + " " + Name;
            }
        }
        public bool IsEnabled { get; set; }
        public IList<JournalEntryLineDetailsViewModel> Lines { get; set; }
        public string Name { get; set; }
        public string Number { get; set; }
        public AccountType Type { get; set; }
    }

    #endregion

    #region Edit

    public class FinanceAccountEditViewModel : AuditableEntityViewModel
    {
        public FinanceAccountEditViewModel(Account account)
        {
            CreatedBy = account.CreatedBy;
            CreatedDate = account.CreatedDate;
            Description = account.Description;
            Id = account.Id;
            IsEnabled = account.IsEnabled;
            Name = account.Name;
            Number = account.Number;
            LastModifiedBy = account.LastModifiedBy;
            LastModifiedDate = account.LastModifiedDate;
            Type = account.Type;
        }

        public FinanceAccountEditViewModel()
        {
        }

        public string CompositeId
        {
            get
            {
                return String.Join("-", Descriptive, Number) + " " + Name;
            }
        }
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }
        public override string Descriptive
        {
            get { return "ACT"; }
        }
        [Display(Name = "Enabled")]
        public bool IsEnabled { get; set; }
        [DataType(DataType.Text)]
        [Required]
        [StringLength(256)]
        public string Name { get; set; }
        public string Number { get; set; }
        public AccountType Type { get; set; }

        public Account GetAccount(Account account, string userName)
        {
            DateTime timeStamp = DateTime.Now;

            account.Description = this.Description;
            account.IsEnabled = this.IsEnabled;
            account.LastModifiedBy = userName;
            account.LastModifiedDate = timeStamp;
            account.Name = this.Name;

            return account;
        }
    }

    #endregion
}